<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:09:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceAcceptancePayDetailsRepository;
use App\Repositories\FinanceAcceptancePayRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property FinanceAcceptancePayRepository $repository 
 * @property FinanceAcceptancePayDetailsRepository $details 
 */
class AcceptancePayService extends Service
    {
    public function boot(FinanceAcceptancePayRepository $repository, FinanceAcceptancePayDetailsRepository $details)
        {
        $this->repository = $repository;
        $this->details    = $details;
        }

    public function info()
        {
        return $this->repository->whereId(request("id"))->getOne();
        }

    public function details()
        {
        $data = $this->details->condition()->getGrid();
        return $data;
        }
    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    protected function save()
        {
        $data                   = request()->toArray();
        $data['handletrade_id'] = request('trade_id');
        $issetCompany           = false;
        if (is_numeric($data['customer_id']) && $data['customer_id'] > 0) {
            $issetCompany = true;
            if (in_array($data['subject_id'], [2202, 220202, 220201]))
                fail("填写收款单位，必须是费用科目！");
            }
        if (is_numeric($data['supplier_id']) && $data['supplier_id'] > 0) {
            $issetCompany = true;
            if (!in_array($data['subject_id'], [2202, 220202, 220201]))
                fail("填写应付单位，必须是应付科目！");
            }
        if (!$issetCompany) {
            fail("收款单位和应付单位必须填一个！");
            }
        if (($data['id'] ?? 0) > 0) {
            $old = $this->repository->whereId($data['id'])->first();
            if ($old->status_id != 0) {
                fail("单据{$old->status->name}！无法编辑！");
                }
            }
        $gdata = $this->repository->autosave($data);
        $bank  = [];

        $gdata->details->map(function ($v) use (&$bank)
            {
            $bank[$v->acceptance_rec->bank_remark][$v->acceptance_rec->code] = $v->acceptance_rec->code;
            });
        $bank_remark = [];
        foreach ($bank as $bank_name => $acc_code) {

            $bank_remark[] = $bank_name . implode("", $acc_code);
            }
        $gdata->bank_remark = implode(",", $bank_remark);
        return $gdata->save();
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据已{$gdata->status->name}！不能审核！");
                }
            return $gdata->finance();
            });
        }
    // protected function noaudit()
    //     {
    //     return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
    //         {
    //         if ($gdata->status_id != 1) {
    //             fail("单据已{$gdata->status->name}！不能审核！");
    //             }
    //         auditSave($gdata, false);
    //         });
    //     }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("单据[{$v->status->name}]！禁止删除");
                }
            $v->details->filter(fn($v) => $v->delete());

            return $v->delete();
            });
        }

    }